package com.mspbots.rule.queue.escalation;

import com.mspbots.api.config.ApiProperties;
import com.mspbots.api.service.TenantRuleService;
import com.mspbots.api.service.TenantUserService;
import com.mspbots.cw.request.TriggerEscalationDTO;
import com.mspbots.rule.exceptions.BaseRule;
import com.mspbots.rule.exceptions.triggerEscalation.TriggerEscalationManager;
import com.mspbots.rule.exceptions.triggerEscalation.TriggerEscalationUser;
import com.mspbots.rule.queue.BaseQueue;
import lombok.extern.slf4j.Slf4j;
import org.jeasy.rules.api.Facts;
import org.jeasy.rules.api.Rules;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author Dennis
 */
@Slf4j
@Component
public class EscalationQueue extends BaseQueue {

    private static final String User_ruleName = "TriggerEscalationUser";
    private static final String Manager_ruleName = "TriggerEscalationManager";
    @Autowired
    private TenantUserService tenantUserService;
    @Autowired
    private TenantRuleService tenantRuleService;
    @Autowired
    private ApiProperties apiProperties;

    @Async
    @RabbitListener(queues = "trigger.escalation.EscalationUser")
    protected void onRuleUser(List<TriggerEscalationDTO> triggerEscalationDTOList) {
        for (TriggerEscalationDTO triggerEscalationDTO : triggerEscalationDTOList) {
            try {

                Rules rules = new Rules();
                Class clazz = Class.forName("com.mspbots.rule.exceptions.triggerEscalation.TriggerEscalationUser");
                TriggerEscalationUser instance = (TriggerEscalationUser) clazz.getDeclaredConstructor().newInstance();
                instance.init(messageProducer, messagesService, tenantUserMappingService, tenantConfigurationService, coreClient, objectMapper);
                instance.setTenantUserService(tenantUserService);
                instance.setTenantRuleService(tenantRuleService);
                instance.setApiProperties(apiProperties);
                rules.register(instance);
                Facts facts = new Facts();
                facts.put(BaseRule.FACT, triggerEscalationDTO);
                rulesEngine.fire(rules, facts);
            } catch (Exception e) {
                log.error("fire error : {}", User_ruleName, e.getMessage());
                e.printStackTrace();
            }
        }
    }

    @Async
    @RabbitListener(queues = "trigger.escalation.EscalationManager")
    protected void onRuleManager(List<TriggerEscalationDTO> triggerEscalationDTOList) {
        for (TriggerEscalationDTO triggerEscalationDTO : triggerEscalationDTOList) {
            try {
                Rules rules = new Rules();
                Class clazz = Class.forName("com.mspbots.rule.exceptions.triggerEscalation.TriggerEscalationManager");
                TriggerEscalationManager instance = (TriggerEscalationManager) clazz.getDeclaredConstructor().newInstance();
                instance.init(messageProducer, messagesService, tenantUserMappingService, tenantConfigurationService, coreClient, objectMapper);
                instance.setTenantUserService(tenantUserService);
                instance.setTenantRuleService(tenantRuleService);
                instance.setApiProperties(apiProperties);
                rules.register(instance);
                Facts facts = new Facts();
                facts.put(BaseRule.FACT, triggerEscalationDTO);
                rulesEngine.fire(rules, facts);
            } catch (Exception e) {
                log.error("fire error : {}", Manager_ruleName, e.getMessage());
                e.printStackTrace();
            }
        }
    }
}
